/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.web.servlet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;

/**
 * 一个 {@link ServletContextInitializer}，用于在 Servlet 3.0+ 容器中注册 {@link DelegatingFilterProxy}。
 * 类似于 {@link ServletContext#addFilter(String, Filter)} 提供的注册功能，
 * 但设计上更适合 Spring Bean。
 * <p>
 * 需要通过构造参数指定实际委托的 {@link Filter} 的 Bean 名称 {@code targetBeanName}。
 * 与 {@link FilterRegistrationBean} 不同，被引用的过滤器不会提前实例化。
 * 实际上，如果委托的过滤器 Bean 标记为 {@code @Lazy}，则只有在过滤器被调用时才会实例化。
 * <p>
 * 注册可以关联 {@link #setUrlPatterns URL 模式} 和/或 Servlet（通过 {@link #setServletNames 名称}
 * 或 {@link #setServletRegistrationBeans ServletRegistrationBean}）。
 * 如果未指定 URL 模式或 Servlet，则过滤器将关联到 '/*'。
 * 如果未另行指定，targetBeanName 将用作过滤器名称。
 *
 * @author Phillip Webb
 * @since 1.4.0
 * @see ServletContextInitializer
 * @see ServletContext#addFilter(String, Filter)
 * @see FilterRegistrationBean
 * @see DelegatingFilterProxy
 */
public class DelegatingFilterProxyRegistrationBean extends AbstractFilterRegistrationBean<DelegatingFilterProxy>
		implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	private final String targetBeanName;

	/**
	 * 创建一个新的 {@link DelegatingFilterProxyRegistrationBean} 实例，并注册指定的
	 * {@link ServletRegistrationBean}。
	 * @param targetBeanName 要在 Spring 应用上下文中查找的目标过滤器 Bean 名称（不能为空）
	 * @param servletRegistrationBeans 关联的 {@link ServletRegistrationBean} 数组
	 */
	public DelegatingFilterProxyRegistrationBean(String targetBeanName,
			ServletRegistrationBean<?>... servletRegistrationBeans) {
		super(servletRegistrationBeans);
		Assert.hasLength(targetBeanName, "TargetBeanName must not be null or empty");
		this.targetBeanName = targetBeanName;
		setName(targetBeanName);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	protected String getTargetBeanName() {
		return this.targetBeanName;
	}

	@Override
	public DelegatingFilterProxy getFilter() {
		return new DelegatingFilterProxy(this.targetBeanName, getWebApplicationContext()) {

			@Override
			protected void initFilterBean() throws ServletException {
				// 不在初始化阶段初始化 Filter Bean
			}

		};
	}

	private WebApplicationContext getWebApplicationContext() {
		Assert.notNull(this.applicationContext, "ApplicationContext be injected");
		Assert.isInstanceOf(WebApplicationContext.class, this.applicationContext);
		return (WebApplicationContext) this.applicationContext;
	}

}
